package com.mine.async.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mine.async.annotation.DownloadRetry;
import com.mine.async.domain.pojo.DataTestBO;
import com.mine.async.domain.request.CreateJobRequest;
import com.mine.async.domain.vo.DataTestVO;
import com.mine.async.jobexe.DownloadJobExecutor;
import com.mine.async.jobexe.PageRespBase;
import com.mine.async.service.IDataTestService;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("/download")
public class DownloadController {

    @Resource(name = "downloadJobExecutorSimple")
    private DownloadJobExecutor downloadJobExecutor;

    @Resource
    private IDataTestService dataTestService;


    @PostMapping("/create-job-1")
    @DownloadRetry(beanClass = DownloadController.class, paramClass = CreateJobRequest.class)
    public String createJob1(@RequestBody CreateJobRequest request) {
        this.getClass().getTypeName();
        return downloadJobExecutor.createJob(() -> queryPageDataTest(request), request);
    }

    @PostMapping("/create-job-2")
    @DownloadRetry(beanClass = DownloadController.class, paramClass = CreateJobRequest.class)
    public String createJob2(@RequestBody CreateJobRequest request) {
        return downloadJobExecutor.createJob(p -> queryPageDataTest((CreateJobRequest) p), request);
    }

    @PostMapping("/create-job-3")
    @DownloadRetry(beanClass = DownloadController.class, paramClass = CreateJobRequest.class)
    public String createJob3(@RequestBody CreateJobRequest request) {
        return downloadJobExecutor.createJobGitHub(p -> queryPageDataTest3((CreateJobRequest) p), request);
    }

    private Page<DataTestVO> queryPageDataTest(CreateJobRequest request) {
        LambdaQueryWrapper<DataTestBO> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.likeRight(DataTestBO::getC2, request.getKeyword());
        lambdaQuery.orderByDesc(DataTestBO::getId);
        Page<DataTestBO> page = dataTestService.page(new Page<>(request.getPageNo(), request.getPageSize()), lambdaQuery);
        Page<DataTestVO> pageVo = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageVo.setRecords(BeanUtil.copyToList(page.getRecords(), DataTestVO.class));
        return pageVo;
    }

    private Page<DataTestVO> queryPageDataTest2(CreateJobRequest request) {
        Page<DataTestBO> page = dataTestService.queryPage(request);
        Page<DataTestVO> pageVo = new Page<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageVo.setRecords(BeanUtil.copyToList(page.getRecords(), DataTestVO.class));
        return pageVo;
    }

    private PageRespBase<DataTestVO> queryPageDataTest3(CreateJobRequest request) {
        Page<DataTestBO> page = dataTestService.queryPage(request);
        PageRespBase<DataTestVO> pageVo = new PageRespBase<>(page.getCurrent(), page.getSize(), page.getTotal());
        pageVo.setList(BeanUtil.copyToList(page.getRecords(), DataTestVO.class));
        return pageVo;
    }

    @PostMapping("/query-page-sql")
    //@DownloadRetry(beanClass = DownloadController.class, paramClass = CreateJobRequest.class)
    public String queryPageSql(@RequestBody CreateJobRequest request) {
        return JSONUtil.toJsonStr(queryPageDataTest2(request));
    }
}
